// const Crawler = require('crawler');
// var toMarkdown = require('to-markdown');
// let articlesLink = [];
// let articles = []
const puppeteer = require('puppeteer')
var fs = require("fs");
var path = require('path')
// async function addCookies(cookies_str, page, domain){
//   let cookies = cookies_str.split(';').map(pair=>{
//     let name = pair.trim().slice(0,pair.trim().indexOf('='))
//     let value = pair.trim().slice(pair.trim().indexOf('=')+1)
//     return {name,value,domain}
//   });
//   await Promise.all(cookies.map(pair=>{
//     return page.setCookie(pair)
//   }))
// }
const basePath = path.resolve(__dirname, '../qijie/qijiechuanshuo.html')
var index = 1
async function write(t){
  // index++
  // var writerStream = fs.createWriteStream(path.resolve(basePath,`qijiechuanshuo.html`));
  // writerStream.write('<h3>'+t.title+'</h3>','UTF8');
  // writerStream.write(t.content,'UTF8');
  // // 标记文件末尾
  // writerStream.end();
  // // 处理流事件 --> data, end, and error
  // writerStream.on('finish', function() {
  //     console.log("写入完成。");
  // });
  try {
    fd = fs.openSync(basePath,'a');
    fs.appendFileSync(fd, '<h3>'+t.title+'</h3>', 'utf8');
    fs.appendFileSync(fd, t.content, 'utf8');
  } catch (err) {
    /* Handle the error */
  } finally {
    if (fd !== undefined)
      fs.closeSync(fd);
  }
}


async function init(){
  const browser = await puppeteer.launch({
    hadeless: false,
    executablePath: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
  })
  const page = await browser.newPage();//生成一个新页面
  await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134');
  // await addCookies(cookiestr,page,'www.lagou.com');//addCookies为自定义函数，具体实现在下面
  page.on('console', msg => console.log('PAGE LOG:', msg.text()));

  let haspage = true
  let url = `https://www.biquge.cc/html/48/48839/3741851.html`
  while(url){
    await page.goto(url,{waitUntil:'networkidle2'});//页面进入目标地址，后一参数表示等待页面加载完毕
    let t = await page.evaluate(()=>{
      var charper ={
        title: '',
        content: '',
        nextUrl: ''
      }
      let $ = window.$;//获取jQuery
      console.log($('.bookname h1').text())
      charper.title = $('.bookname h1').text()
      charper.content = $('#content').html()
      if($('#A3')){
        charper.nextUrl = $('#A3').attr('href')
      }
      let str = '<script>chaptererror();</script><div align="center"><a href="javascript:postError();" style="text-align:center;color:red;">章节错误,点此举报(免注册)</a>,举报后维护人员会在两分钟内校正章节内容,请耐心等待,并刷新页面。</div>'
      charper.content = charper.content.replace(str,'')
      return charper
    })
    await write(t)
    url =  `https://www.biquge.cc/html/48/48839/${t.nextUrl}.html`
  }
  

  await browser.close();//关闭浏览器

}

init()



